<%-- 
    Document   : page
    Created on : 2014/8/21, 上午 09:29:43
    Author     : LENOVO
--%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
    String username = request.getParameter("username");
    String where="";
    if(username != null && !"".equals(username.trim())){
        where ="where username like '%"+username+"%'";
    }
    dao.BaseDAO y = new dao.BaseDAO();
    String sql ="select count(*) as total from person " +where;
    String ary[]={};
    ArrayList list = y.query(sql , ary);
    String total = (String)((HashMap)list.get(0)).get("total");
    
    int pagesize = 10;//每頁筆數
    int pages = Integer.parseInt(total) / pagesize;//總頁數
    if(Integer.parseInt(total) % pagesize != 0)
        pages++;
    
    String pageNo = request.getParameter("pageNo");
    if(pageNo == null)
        pageNo="1";
    int index = (Integer.parseInt(pageNo)-1) * pagesize;
    
    sql="select * from person "+where +" limit "+ index +", "+ pagesize;
    //ary=new String[]{index+"" , pagesize+""  };
    list = y.query(sql, ary);
    System.out.println(list);
    
    int lastPage = Integer.parseInt(pageNo)==1 ? 1 : Integer.parseInt(pageNo)-1;
    int nextpage = Integer.parseInt(pageNo)==pages ? pages : Integer.parseInt(pageNo)+1;
%>
<c:set var="list" value="<%= list %>" />
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form action="page.jsp" id="form1">
            <input type="text" name="username" value="${param.username}">
            <input type="hidden" name="pageNo" id="pageNo" value="1">
            <input type="submit" value="查詢"> 
        <input type="button" value="上一頁" id="pageNo" onclick="doPage('<%= lastPage %>')">
        <!--<a href="page.jsp?pageNo=<%= lastPage %>">上一頁</a>-->
        共有<%= total %>筆資料 , 目前頁碼:<%= pageNo %> , 每頁<%= pagesize %> , 共有<%= pages %>頁
        <!--<a href="page.jsp?pageNo=<%= nextpage %>">下一頁</a>-->
        <form action="page.jsp" id="form3"> 
        <input type="button" value="下一頁" id="pageNo" onclick="doPage('<%= nextpage %>')">
        <select id="pages" onchange="doPage('<%= pages %>')">
            <%
                for(int i =0 ; i<pages ; i++)
                {
            %>
                    <option> <%= i+1 %> </option>
            <%
                }
            %>
        </select>        
    
        
        </form>
        <br>
        <c:forEach var="x" items="${list}">
            <c:out value="${x.id}"/> ,
            <c:out value="${x.username}"/> ,
            <c:out value="${x.tel}"/> ,
            <c:out value="${x.birthday}"/> <br>
        </c:forEach>
        
        <script>
            function doPage(x)
            {
                document.getElementById('pageNo').value=x;
                document.getElementById('form1').submit();
            }       
        </script>
                
    </body>
</html>
